<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">融媒中心管理系统</h3>
      <el-form-item prop="phone">
        <el-input v-model="loginForm.phone" type="text" auto-complete="off" placeholder="手机号">
          <i slot="prefix" class="el-icon-phone"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 65%">
          <i slot="prefix" class="el-icon-burger"></i>
        </el-input>
        <el-button style="margin-left: 10px;width: 30%" size="medium" type="success" :disabled="smsCodeDisabled" :loading="smsCodeLoading" @click.native.prevent="getSmsCode">{{ smsCode }}</el-button>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div class="el-login-footer">
    </div>
  </div>
</template>
<script>
import {
  sendLoginSmsCode
} from "@/api/login";
export default {
  name: "login",
  data() {
    const validatePhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('手机号为空'));
      } else {
        const reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!reg.test(value)) {
          callback(new Error('手机号不正确'));
        } else {
          callback();
        }
      }
    };
    return {
      timer:null,
      smsCodeDisabled:false,
      smsCodeLoading:false,
      smsCode:'获取验证码',
      sliderNum:0,
      codeUrl: "",
      loginForm: {
        phone: "",
        code: "",
      },
      loginRules: {
        phone: [
          { validator: validatePhone, trigger: "blur"},
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {

  },
  methods: {
    getSmsCode(){
      if(this.loginForm.phone){
        const reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!reg.test(this.loginForm.phone)) {
          this.$message({
            type: 'warning',
            message: '手机号不正确'
          });
          return;
        }
      } else {
        this.$message({
          type: 'warning',
          message: '手机号为空'
        });
        return;
      }
      if (!this.timer) {
        this.smsCode = 60;
        this.smsCodeLoading = true;
        this.smsCodeDisabled = true;
        this.timer = setInterval(() => {
          if (this.smsCode > 0 && this.smsCode <= 60) {
            this.smsCode--;
          } else {
            this.smsCodeLoading = false;
            this.smsCodeDisabled = false;
            this.smsCode = '获取验证码';
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000)
        sendLoginSmsCode({phone:this.loginForm.phone}).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '发送成功'
            });
          }
        })
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch("LoginBySms", this.loginForm).then(res => {
            if(res && res.code===200) {
              this.$router.push({
                path: '/index'
              })
            }
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
